package com.syj.collect.adapter;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.syj.collect.app.R;
import com.syj.collect.listnner.MyItemListener;
import com.syj.collect.utils.HttpUtils;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.internal.Util;

public class MyPicAdapter extends RecyclerView.Adapter<MyPicAdapter.MyPicViewHoder> {


    private MyItemListener listenner;
    private List<String> pList = new ArrayList<>();

    public void setonClick(MyItemListener listener) {
        this.listenner = listener;
    }

    @NonNull
    @Override
    public MyPicAdapter.MyPicViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pic, parent, false);
        return new MyPicViewHoder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyPicAdapter.MyPicViewHoder holder, int position) {


        switch (pList.size() - position) {
            case 1:
                Glide.with(holder.itemView.getContext()).load(pList.get(position)).into(holder.img);
                break;
            case 2:
                Glide.with(holder.itemView.getContext()).load(pList.get(position)).into(holder.img);
                break;
            case 3:

                Glide.with(holder.itemView.getContext()).load(pList.get(position)).into(holder.img);
                break;
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenner != null) listenner.onmListenner(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    public void upDate(Context context, List pic) {

        if (pic.size() == 3) pList.clear();
        for (int i = 0; i < pic.size(); i++) {
//            pList.add(getRealPathFromUri(context, Uri.parse(pic.get(i).toString())));
            pList.add(getRealFilePath(context, Uri.parse(pic.get(i).toString())));
        }

        notifyDataSetChanged();
    }

    public void upDate(List<String> pic) {

        pList.clear();
        for (int i = 0; i < pic.size(); i++) {
            pList.add(HttpUtils.ImgURL + pic.get(i));
        }
//        Log.e("list", pic.size() + "  " + pList.size() + "  " + pic.get(0));
//        Log.e("list", pic.size() + "  " + pList.size() + "  " + pList.get(0));
        notifyDataSetChanged();
    }

    public void upDate(int pos, String pic) {
        try {
            pList.set(pos, pic);
            notifyItemChanged(pos);
        } catch (Exception e) {
            pList.add(pic);
            notifyDataSetChanged();
        }



    }

    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = {MediaStore.Images.Media.DATA};
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    /**
     * 根据Uri获取文件真实地址
     */
    public static String getRealFilePath(Context context, Uri uri) {
        if (null == uri) return null;
        final String scheme = uri.getScheme();
        String realPath = null;
        if (scheme == null)
            realPath = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            realPath = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri,
                    new String[]{MediaStore.Images.ImageColumns.DATA},
                    null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        realPath = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        if (TextUtils.isEmpty(realPath)) {
            if (uri != null) {
                String uriString = uri.toString();
                int index = uriString.lastIndexOf("/");
                String imageName = uriString.substring(index);
                File storageDir;

                storageDir = Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES);
                File file = new File(storageDir, imageName);
                if (file.exists()) {
                    realPath = file.getAbsolutePath();
                } else {
                    storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
                    File file1 = new File(storageDir, imageName);
                    realPath = file1.getAbsolutePath();
                }
            }
        }
        return realPath;
    }

    public List<String> getPicList() {
        return pList;
    }

    /**
     *
     */
    class MyPicViewHoder extends RecyclerView.ViewHolder {
        private final ImageView img;

        public MyPicViewHoder(@NonNull View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.item_img);
        }
    }
}
